﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GMS.BIZ;

namespace Web.SetSystem
{
    public partial class WebForm9 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!((Account)HttpContext.Current.Session["Account"]).RoleCurrent.Equals('a')) Response.Redirect("Default.aspx");
            if (!IsPostBack)
            {
                refreshDocStatus();
                refreshDeadlines();
            }
        }

        protected void refreshDocStatus()
        {
            ArrayList myAL = Library.GetDocumentStatusLib();
            GridView_docstatus.DataSource = myAL;
            GridView_docstatus.DataBind();
        }

        protected void refreshDeadlines()
        {
            DeadLine myDl = new DeadLine();
            DataSet myDs = myDl.GetDataSet();
            DataColumn myDc = new DataColumn("use", System.Type.GetType("System.String"));
            myDs.Tables[0].Columns.Add(myDc);
            for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
            {
                if (myDs.Tables[0].Rows[i]["inUse"].ToString() == "1") myDs.Tables[0].Rows[i].SetField("use", "是");
                else myDs.Tables[0].Rows[i].SetField("use", "否");
            }
            GridView_deadline.DataSource = myDs;
            GridView_deadline.DataBind();


        }

        protected void ImageButton_addstatus_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Label_result1.Text = myLb.AddDocumentStatus(TextBox_statusname.Text.ToString());

            refreshDocStatus();
        }

        protected void ImageButton_delstatus_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Label_result1.Text = myLb.DeleteDocumentStatus(TextBox_statusname.Text.ToString());

            refreshDocStatus();
        }

        protected void GridView_deadline_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DeadLine myDl = new DeadLine();
            Label_result2.Text = myDl.Delete(((Label)(GridView_deadline.Rows[e.RowIndex].Cells[0].FindControl("Label1"))).Text.ToString());

            refreshDeadlines();            
        }

        protected void GridView_deadline_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView_deadline.EditIndex = e.NewEditIndex;

            refreshDeadlines();
            DataSet myDS = (DataSet)GridView_deadline.DataSource;
            if (myDS.Tables[0].Rows[e.NewEditIndex]["inUse"].ToString()=="1")
            ((DropDownList)GridView_deadline.Rows[e.NewEditIndex].Cells[7].FindControl("DropDownList1")).SelectedIndex=1;
            else ((DropDownList)GridView_deadline.Rows[e.NewEditIndex].Cells[7].FindControl("DropDownList1")).SelectedIndex = 0;
        }             

        protected void GridView_deadline_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DeadLine myDl = new DeadLine();
            DeadLineInfo myDli = new DeadLineInfo();
            myDli.gyear = ((Label)(GridView_deadline.Rows[e.RowIndex].Cells[0].FindControl("Label_edit1"))).Text;
            try
            {
                myDli.deadline1 = DateTime.Parse(((TextBox)(GridView_deadline.Rows[e.RowIndex].Cells[1].FindControl("TextBox2"))).Text);
                myDli.deadline2 = DateTime.Parse(((TextBox)(GridView_deadline.Rows[e.RowIndex].Cells[2].FindControl("TextBox3"))).Text);
                myDli.deadline3 = DateTime.Parse(((TextBox)(GridView_deadline.Rows[e.RowIndex].Cells[3].FindControl("TextBox4"))).Text);
                myDli.deadline4 = DateTime.Parse(((TextBox)(GridView_deadline.Rows[e.RowIndex].Cells[4].FindControl("TextBox5"))).Text);
                myDli.deadline5 = DateTime.Parse(((TextBox)(GridView_deadline.Rows[e.RowIndex].Cells[4].FindControl("TextBox6"))).Text);
                myDli.deadline6 = DateTime.Parse(((TextBox)(GridView_deadline.Rows[e.RowIndex].Cells[4].FindControl("TextBox7"))).Text);
                if (((DropDownList)(GridView_deadline.Rows[e.RowIndex].Cells[5].FindControl("DropDownList1"))).SelectedValue.CompareTo("是") == 0)
                    myDli.inUse = 1;
                else myDli.inUse = 0;
                Label_result2.Text = myDl.Update(myDli);

                GridView_deadline.EditIndex = -1;
                refreshDeadlines();
            }
            catch (Exception exp)
            {
                Label_result2.Text = exp.Message;
            }
        }

        protected void GridView_deadline_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView_deadline.EditIndex = -1;
            refreshDeadlines();
        }

        protected void ImageButton_adddl_Click(object sender, ImageClickEventArgs e)
        {
            DeadLine myDl = new DeadLine();
            DeadLineInfo myDli = new DeadLineInfo();
            myDli.gyear = TextBox_gyear.Text;
            try
            {
                myDli.deadline1 = DateTime.Parse(TextBox_dl1.Text);
                myDli.deadline2 = DateTime.Parse(TextBox_dl2.Text);
                myDli.deadline3 = DateTime.Parse(TextBox_dl3.Text);
                myDli.deadline4 = DateTime.Parse(TextBox_dl4.Text);
                myDli.deadline5 = DateTime.Parse(TextBox_dl5.Text);
                myDli.deadline6 = DateTime.Parse(TextBox_dl6.Text);
                if (CheckBox_inuse.Checked == true) myDli.inUse = 1;
                else myDli.inUse = 0;
                Label_result3.Text = myDl.Add(myDli);

                refreshDeadlines();
                TextBox_gyear.Text = "";
                TextBox_dl1.Text = "";
                TextBox_dl2.Text = "";
                TextBox_dl3.Text = "";
                TextBox_dl4.Text = "";
                TextBox_dl5.Text = "";
                TextBox_dl6.Text = "";
                CheckBox_inuse.Checked = false;


            }
            catch (Exception exp)
            {
                Label_result3.Text = exp.Message;
            }

        }

        protected void GridView_docstatus_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_docstatus.PageIndex = e.NewPageIndex;
            refreshDocStatus();
        }

        protected void GridView_deadline_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_deadline.PageIndex = e.NewPageIndex;
            refreshDeadlines();
        }
    }
}
